﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.PeiXunService;

namespace CommunitySystem
{
    public partial class PeiXunAdminWindow : ChildWindow
    {
        private PeiXunService.PeiXunServiceClient client1 = new PeiXunService.PeiXunServiceClient();
        private int peixunid = 0;
        private PeiXunService.PeiXunDetailModel model = new PeiXunService.PeiXunDetailModel();
        private ObservableCollection<PeiXunDetailModel> modellist = new ObservableCollection<PeiXunDetailModel>();
        private int olddingpiaoshu = 0;
        public PeiXunAdminWindow(int id)
        {
            InitializeComponent();
            peixunid = id;
            org.LostFocus += new RoutedEventHandler(org_LostFocus);
            client1.GetPeiXunDetailModelCompleted += new EventHandler<PeiXunService.GetPeiXunDetailModelCompletedEventArgs>(client1_GetPeiXunDetailModelCompleted);
            client1.GetPeiXunDetailModelAsync("0", 0, id);
            client1.GetPeiXunDetailModelForAdminCompleted += new EventHandler<GetPeiXunDetailModelForAdminCompletedEventArgs>(client1_GetPeiXunDetailModelForAdminCompleted);
            client1.CanDingPiaoCompleted += new EventHandler<PeiXunService.CanDingPiaoCompletedEventArgs>(client1_CanDingPiaoCompleted);
            client1.AddPeiXunDetailModelCompleted += new EventHandler<PeiXunService.AddPeiXunDetailModelCompletedEventArgs>(client1_AddPeiXunDetailModelCompleted);
        }
        private int yijingpiaoshu = 0;
        void client1_GetPeiXunDetailModelForAdminCompleted(object sender, GetPeiXunDetailModelForAdminCompletedEventArgs e)
        {
            if (e.Result == 0)
            {
                yudingzhangshu1.Visibility = System.Windows.Visibility.Collapsed;
                yudingzhangshu.Visibility = System.Windows.Visibility.Collapsed;
                yudingzhangshu2.Visibility = System.Windows.Visibility.Collapsed;
            }
            else
            {
                yudingzhangshu.Visibility = System.Windows.Visibility.Visible;
                yudingzhangshu1.Visibility = System.Windows.Visibility.Visible;
                yudingzhangshu2.Visibility = System.Windows.Visibility.Visible;
                yijingpiaoshu = e.Result;
                yudingzhangshu.Text = e.Result.ToString();
                olddingpiaoshu = e.Result;
            }
        }

        void client1_GetPeiXunDetailModelCompleted(object sender, PeiXunService.GetPeiXunDetailModelCompletedEventArgs e)
        {
            if (e.Result != null && e.Result.Count > 0)
            {
                foreach (PeiXunDetailModel i in e.Result)
                {
                    if (i.OrgID == 0) modellist.Add(i);
                }
                if (modellist.Count == 0)
                {
                    model = e.Result[0];
                    this.DataContext = e.Result[0];
                }
                else
                {
                    model = modellist[0];
                    model.PeiXunID = peixunid;
                    this.DataContext = modellist[0];
                }
            }
            
        }

        void org_LostFocus(object sender, RoutedEventArgs e)
        {
            client1.GetPeiXunDetailModelForAdminAsync(peixunid, org.Text);
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            if (model.DingPiaoShu == 0)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请填写预定票数！");
                return;
            }
            client1.CanDingPiaoAsync(model, olddingpiaoshu);
        }
        void client1_CanDingPiaoCompleted(object sender, PeiXunService.CanDingPiaoCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                model.OrgID = 0;
                model.OrgName = org.Text;
                client1.AddPeiXunDetailModelAsync(model);
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("所选培训票数不足！余票为" + model.YuPiao + "张");
                return;
            }
        }
        
        void client1_AddPeiXunDetailModelCompleted(object sender, PeiXunService.AddPeiXunDetailModelCompletedEventArgs e)
        {
            if (e.Result == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("订票成功！");
                //result();
                this.DialogResult = true;
            }
        }

        private string pattern = @"^[0-9]+([.]{1}[0-9]+){0,1}$";
        private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            Match m = Regex.Match(((TextBox)sender).Text, pattern); // 匹配正则表达式
            if (!m.Success) // 输入的不是数字
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("只能输入数字！");
                ((TextBox)sender).Text = "";
                return;
            }
        }
    }
}
